package com.zzzzzz.common.page;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

public class Page<T> {
	private int pageNo = 1;
	private int pageSize = -1;
	private String url;
	private boolean autoCount = true;
	
	//-- 返回结果 --//
	private List<T> result;
	private long totalCount = -1;
	
	private String bar;
	
	public Page(HttpServletRequest request, String url, int pageSize){
		String pageNoParam = request.getParameter("page");
		if(StringUtils.isNotBlank(request.getParameter("page"))){
			setPageNo(Integer.parseInt(pageNoParam));
		}
		setUrl(request.getContextPath()+url);
		setPageSize(pageSize);
	}
	
	//-- 分页参数访问函数 --//
	/**
	 * 获得当前页的页号,序号从1开始,默认为1.
	 */
	public int getPageNo() {
		return pageNo;
	}

	/**
	 * 设置当前页的页号,序号从1开始,低于1时自动调整为1.
	 */
	public void setPageNo(final int pageNo) {
		this.pageNo = pageNo;

		if (pageNo < 1) {
			this.pageNo = 1;
		}
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	/**
	 * 获得每页的记录数量, 默认为-1.
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置每页的记录数量.
	 */
	public void setPageSize(final int pageSize) {
		this.pageSize = pageSize;
	}
	
	//-- 访问查询结果函数 --//

	/**
	 * 获得页内的记录列表.
	 */
	public List<T> getResult() {
		return result;
	}

	/**
	 * 设置页内的记录列表.
	 */
	public void setResult(final List<T> result) {
		this.result = result;
	}

	/**
	 * 获得总记录数, 默认值为-1.
	 */
	public long getTotalCount() {
		return totalCount;
	}

	/**
	 * 设置总记录数.
	 */
	public void setTotalCount(final long totalCount) {
		this.totalCount = totalCount;
	}

	public int getOffset(){
		return pageSize*(pageNo-1);
	}

	public void setBar(String bar) {
		this.bar = bar;
	}
	
	public String getBar(){
		return bar;
	}

	public boolean isAutoCount() {
		return autoCount;
	}

	public void setAutoCount(boolean autoCount) {
		this.autoCount = autoCount;
	}
	
	
	
}
